package com.shiyingzi.dictionary.service.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.shiyingzi.auser.model.AUser;
import com.shiyingzi.common.EntityUtils;
import com.shiyingzi.common.PageUtils;
import com.shiyingzi.common.PageableData;
import com.shiyingzi.dictionary.dao.DictionaryDao;
import com.shiyingzi.dictionary.model.Dictionary;
import com.shiyingzi.dictionary.service.IDictionaryService;


@Service(IDictionaryService.BEAN_ID)
public class DictionaryServiceImpl extends ServiceImpl<DictionaryDao, Dictionary> implements IDictionaryService{
	
	@Autowired
	DictionaryDao dicDao;
	@Autowired
	HttpSession session;
	
	@Override
	public PageableData<Dictionary> doQuery(int page, int limit, String dicName) {
		Page<Dictionary> mypage = new Page<Dictionary>();
		mypage.setCurrent(page);
		mypage.setSize(limit);
		Dictionary ac =new Dictionary();
		if(!StringUtils.isEmpty(dicName)){
			ac.setDicValue(dicName);
		}
		return PageUtils.tableDataDto(selectPage(mypage,new EntityWrapper<Dictionary>(ac).orderBy("createTime")));
	}

	@Override
	public List<Dictionary> getDictionaryByType(String type) {
		return dicDao.selectList(new EntityWrapper<Dictionary>().eq("dic_type", type));
	}

	@Override
	public List<Dictionary> getDictionaryByTypeAndKey(String type, String key) {
		
		return dicDao.selectList(new EntityWrapper<Dictionary>().eq("dic_type", type).and().eq("dic_key", key));
	}

	@Override
	public Dictionary getDictionaryById(int id) {
		return selectById(id);
	}

	@Override
	public Dictionary addDictionary(Dictionary dic) {
		EntityUtils.initEntity(dic, session);
		dicDao.insert(dic);
		return dic;
	}

	@Override
	public Dictionary delDictionary(Dictionary dic) {
		EntityUtils.deleteEntity(dic, session);
		updateById(dic);
		return dic;
	}

	@Override
	public Dictionary modifyDictionary(Dictionary dic) {
		EntityUtils.modifyEntity(dic, session);
		updateById(dic);
		return dic;
	}

	

}
